package com.lingeringdream.luciole.helper.list;

import com.lingeringdream.luciole.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * options list helper
 *
 * @author Kirky-X
 */
public class OptionListHelper {
    private final List<Integer> mItemIcon = new ArrayList<>();
    private final List<String> mItemTitle = new ArrayList<>();
    private final List<String> mItemValue = new ArrayList<>();
    private final List<ClickedListener> mItemListeners = new ArrayList<>();
    private int itemNum = 0;
    private DirectionalLayout mLayout;
    private boolean setIcon = false;
    private boolean setLayout = false;
    private int layoutResourceId = ResourceTable.Layout_layout_option_list_normal;

    public interface ClickedListener {
        /**
         * 点击监听器
         */
        void onClick(OptionListHelper listener);
    }

    public static class Builder {
        private final OptionListHelper mOptionList;
        private final Context mContext;

        public Builder(Context context) {
            this.mContext = context;
            this.mOptionList = new OptionListHelper();
        }

        public Builder setDirectionalLayout(DirectionalLayout layout) {
            mOptionList.mLayout = layout;
            return this;
        }

        public Builder setIcon(boolean set) {
            mOptionList.setIcon = set;
            return this;
        }

        private Builder setItemLayout(int layoutResourceId) {
            mOptionList.setLayout = true;
            mOptionList.layoutResourceId = layoutResourceId;
            return this;
        }

        public Builder addItem(String value, ClickedListener listener) {
            mOptionList.mItemValue.add(value);
            mOptionList.mItemListeners.add(listener);
            mOptionList.itemNum++;
            return this;
        }

        public Builder addItem(int iconId, String value, ClickedListener listener) {
            mOptionList.mItemIcon.add(iconId);
            mOptionList.mItemValue.add(value);
            mOptionList.mItemListeners.add(listener);
            mOptionList.itemNum++;
            return this;
        }

        public void create() {
            if (!mOptionList.setLayout) {
                if (mOptionList.setIcon) {
                    for (int i = 0; i < mOptionList.itemNum; ++i) {
                        Component item = newItem(mOptionList.mItemIcon.get(i), mOptionList.mItemValue.get(i));
                        int currentIndex = i;
                        item.setClickedListener(component -> mOptionList.mItemListeners.get(currentIndex).onClick(mOptionList));
                        mOptionList.mLayout.addComponent(item);
                    }
                } else {
                    for (int i = 0; i < mOptionList.itemNum; ++i) {
                        Component item = newItem(mOptionList.mItemValue.get(i));
                        int currentIndex = i;
                        item.setClickedListener(component -> mOptionList.mItemListeners.get(currentIndex).onClick(mOptionList));
                        mOptionList.mLayout.addComponent(item);
                    }
                }
            }
        }

        private DependentLayout newItem(String valueStr) {
            DependentLayout itemLayout = (DependentLayout) LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_option_list_normal, null, false);

            Text value = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_value);
            value.setText(valueStr);

            return itemLayout;
        }

        private DependentLayout newItem(int iconId, String valueStr) {
            DependentLayout itemLayout = (DependentLayout) LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_option_list_normal, null, false);

            Image icon = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_icon);
            VectorElement iconElement = new VectorElement(mContext, iconId);
            icon.setBackground(iconElement);
            icon.setVisibility(Component.VISIBLE);

            Text value = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_value);
            value.setText(valueStr);

            return itemLayout;
        }

        private DependentLayout newItem(int iconId, String titleStr, String valueStr) {
            DependentLayout itemLayout = (DependentLayout) LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_option_list_normal, null, false);

            Image icon = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_icon);
            VectorElement iconElement = new VectorElement(mContext, iconId);
            icon.setBackground(iconElement);
            icon.setVisibility(Component.VISIBLE);

            Text title = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_title);
            title.setText(titleStr);
            title.setVisibility(Component.VISIBLE);

            Text value = itemLayout.findComponentById(ResourceTable.Id_option_list_normal_value);
            value.setText(valueStr);

            return itemLayout;
        }
    }

}
